package com.batchbrake.queue;

import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import org.jdesktop.swingx.JXTable;

import com.batchbrake.main.EFile;

public class SubtitlesTable extends JXTable {
	private DefaultTableCellRenderer subtitleRenderer = new DefaultTableCellRenderer();
	private DefaultTableCellRenderer stringRenderer = new DefaultTableCellRenderer();
	
	private Map<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
	
	public SubtitlesTable(TableModel model) {
		super(model);
		setShowGrid(false);

		stringRenderer.setOpaque(false);
		subtitleRenderer.setOpaque(false);
		subtitleRenderer.setIcon(SubtitlesTableModel.icnSubtitle);
		
		this.loadIcons();
	}

	private void loadIcons() {
		EFile imgFolder = new EFile("images/langs/");
		EFile[] files = imgFolder.listEFiles();
		for( int i = 0; i < files.length; i++ ) {
			icons.put(files[i].getBaseName(), new ImageIcon(files[i].toString()));
		}
	}
	
	@Override
	public TableCellRenderer getCellRenderer(int row, int column) {
		switch( column ) {
		case SubtitlesTableModel.COL_SUBTITLE:
			subtitleRenderer.setIcon(icons.get(this.getValueAt(row, SubtitlesTableModel.COL_LANG)));
			return subtitleRenderer;
		case SubtitlesTableModel.COL_ENCODING:
		case SubtitlesTableModel.COL_LANG:
			return stringRenderer;
		}
		return super.getCellRenderer(row, column);
	}
	
	
	
}
